<?php

namespace App\Http\Middleware;

use Auth, Closure, Redirect;

class AuthLogin
{    
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $guard = getGuard($request->server());
        
        //default guard except 'wx'
        if( strcmp($guard, 'wx') != 0 ) $guard = null;

        if (Auth::guard($guard)->guest()) {
            if ($request->ajax() || $request->wantsJson()) {
                return response('Unauthorized.', 401);
            } else {
                return Redirect::guest('/login');
            }
        }
        return $next($request);
    }
}
